<?php
/**
 * Created by PhpStorm.
 * User: chang
 * Date: 2019/4/1
 * Time: 11:54
 */

namespace App\Http\Model;


use App\Http\Model\Gift;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class MallMain extends Base
{

    #楼层类型
    const MAIN_TYPE_BANNER = 'BANNER';
    const MAIN_TYPE_NAV = 'NAV';
    const MAIN_TYPE_GOODS = 'GOODS';



    protected $primaryKey = "id";
    protected $table = "mall_main";
    protected $tableItem = "mall_item";

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    /**
     * 获取楼层类型
     * @param null $index
     * @return array|mixed|string
     */
    public function getMainType($index=NULL)
    {
        $item = [];
        $item[self::MAIN_TYPE_BANNER] = '广告轮播';
        $item[self::MAIN_TYPE_NAV] = '导航';
        $item[self::MAIN_TYPE_GOODS] = '活动商品';

        if ($index && isset($item[$index])){
            return $item[$index];
        }
        return $item;
    }










    /***********************************楼层处理************************************************/
    /**
     * 添加楼层
     */
    public function addMain($data)
    {
        $data['add_time'] = $data['edit_time'] = time();
        return DB::table($this->table)->insert($data);
    }

    /**
     * 编辑楼层
     */
    public function editMain($where, $data)
    {
        $data['edit_time'] = time();
        return DB::table($this->table)
            ->where($where)
            ->update($data);
    }

    /**
     * 删除楼层
     */
    public function delMain($where)
    {
        return DB::table($this->table)
            ->where($where)
            ->delete();
    }

    /**
     * 统计
     */
    public function countMain($where=[])
    {
        $obj = DB::table($this->table);
        if ($where){
            $obj->where($where);
        }

        return $obj->count();
    }

    public function infoMainById($id)
    {
        if ($id<=0){
            return false;
        }
        $res = DB::table($this->table)->find($id);
        return $this -> objToArray($res);
    }

    public function listMain($where=[]){
        $list = DB::table($this->table)
            ->where($where)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'ASC')
            ->get();

        return $this -> objToArray($list);
    }
    // 楼层主页 API调用
    public function getMallMain(){
        $list = DB::table($this->table)
            ->where('state', '=','1')
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'ASC')
            ->get();

        return $this -> objToArray($list);
    }


    /***********************************元素处理************************************************/

    /**
     * 添加元素
     */
    public function addItem($data)
    {
        $data['add_time'] = $data['edit_time'] = time();
        return DB::table($this->tableItem)->insert($data);
    }

    /**
     * 编辑元素
     */
    public function editItem($where,$data)
    {
        $data['edit_time'] = time();
        return DB::table($this->tableItem)
            ->where($where)
            ->update($data);
    }

    /**
     * 删除元素
     */
    public function delItem($where)
    {
        return DB::table($this->tableItem)
            ->where($where)
            ->delete();
    }

    public function countItem($where=[])
    {
        $obj = DB::table($this->tableItem);
        if ($where){
            $obj->where($where);
        }

        return $obj->count();
    }

    public function infoItemById($id)
    {
        if ($id<=0){
            return false;
        }
        $res = DB::table($this->tableItem)->find($id);
        return $this -> objToArray($res);
    }
    public function listItem($where=[]){
        $list = DB::table($this->tableItem)
            ->where($where)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'ASC')
            ->get();

        return $this -> objToArray($list);
    }

    /**
     * 楼层 元素 api调用 有效可见元素
     * @param $Mainid
     * @return mixed
     */
    public function getMainItem($Mainid)
    {
        $time = time();
        $list = DB::table($this->tableItem)
            ->where('main_id', '=', $Mainid)
            ->where('is_show', '=',1)
            ->where('start_time', '<=',$time)
            ->where('end_time', '>=',$time)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'ASC')
            ->get();

        return $this -> objToArray($list);
    }





}
